package cac.fenjian.resource.wmsitemtransaction.rest;

import cac.fenjian.resource.WmsItemTransaction.dto.WmsItemTransactionDTO;
import cac.fenjian.resource.wmsitemtransaction.service.WmsItemTransactionService;
import com.alibaba.fastjson.JSON;
import com.cac.dme.adapter.rest.msg.PageParameter;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.ContextCommonHolder;
import com.cac.dme.common.utils.JsonHelper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.cac.dme.adapter.utils.BusinessUtil.getSortExpColumnName;

@RestController
@RequestMapping("/wmsItemTransaction")
public class wmsItemTransactionRest {

    @Autowired
    private WmsItemTransactionService wmsItemTransactionService;

    //库存管理流水查询
    @PostMapping("/searchParamsByItem")
    public ResponseMsg<QueryRespBean<WmsItemTransactionDTO>> searchItemTransactionByItem(@RequestBody Map map) {
        ResponseMsg<QueryRespBean<WmsItemTransactionDTO>> responseMsg = new ResponseMsg<>();
        //获取图号
        String itemType = (String) map.get("itemType");
        //时间范围
        String startTimeParams = JSON.toJSONString(map.get("startTime"));
        String endTimeParams = JSON.toJSONString(map.get("endTime"));
        Date startTime = JSON.parseObject(startTimeParams, Date.class);
        Date endTime = JSON.parseObject(endTimeParams, Date.class);
        //分页
        String param = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(param, PageParameter.class);
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        WmsItemTransactionDTO searchParams = JSON.parseObject(searchParamsParams, WmsItemTransactionDTO.class);

        QueryReqBean<WmsItemTransactionDTO> queryReqBean = new QueryReqBean<>();
        queryReqBean.setSearchParams(searchParams);
        queryReqBean.setPageParameter(pageParameter);

        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), WmsItemTransactionDTO.class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            WmsItemTransactionDTO searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<WmsItemTransactionDTO>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<WmsItemTransactionDTO> result = wmsItemTransactionService.searchParamsByBarcodeAM(queryReqBean, ContextCommonHolder.getOrgId(), queryReqBean.getSortExp(), startTime, endTime, itemType);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    // 出入库流水查询
    @PostMapping("outInFlowQuery")
    public PageInfo outInFlowQuery(@RequestBody HashMap param) {
        HashMap queryParam = (HashMap) param.get("queryParam");
        HashMap page = (HashMap) param.get("page");
        int startPage = Integer.parseInt(page.get("currentPage").toString());
        int pageSize = Integer.parseInt(page.get("pageSize").toString());
        return wmsItemTransactionService.outInFlowQuery(startPage, pageSize, queryParam);
    }
}
